package com.blue.seata.order.service;

import com.blue.seata.order.eneity.Order;
import com.blue.seata.order.mapper.OrderMapper;
import io.seata.core.context.RootContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liulei
 * @version 1.0
 */
@Service
public class OrderService {

    private static final Logger logger = LoggerFactory.getLogger(OrderService.class);

    @Autowired
    private OrderMapper mapper;

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void createOrder(String userId, String code, Integer count) {
        String xid = RootContext.getXID();
        Order order = new Order();
        order.setUserId(userId).setCode(code).setCount(count);
        this.mapper.insert(order);
        // 构建人为异常
        if (code.equals("002")) {
            throw new RuntimeException("订单中心人为异常..");
        }
        logger.info("订单中心创建订单[{}],[{}],[{}],[{}]成功", userId, code, count, xid);
    }
}
